tags: Easy、Linked-List
Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
用簡單的移除list node的方式去解,要記得先處理頭指針
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val) {
while (head != NULL && head->val == val) {
struct ListNode *temp = head;
head = head->next;
free(temp);
}
struct ListNode *current = head;
while(current != NULL && current->next != NULL) {
if (current->next->val == val) {
struct ListNode *temp = current->next;
current->next = current->next->next;
free(temp);
}
else {
current = current->next;
}
}
return head;
}